package com.itcast.mallapplet.controller.user;


import com.itcast.admin.entity.Seckill;
import com.itcast.applet.dto.ShoppingDhDTO;
import com.itcast.applet.dto.ShoppingIdDTO;
import com.itcast.applet.entity.Shopping;
import com.itcast.applet.entity.ShoppingDh;
import com.itcast.mallapplet.service.ShoppingService;
import com.itcast.result.PageResult;
import com.itcast.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user/shopping")
@Slf4j
@Api(tags = "用户端-购物车模块")
public class ShoppingController {

    @Autowired
    private ShoppingService shoppingService;

    @PostMapping("/add")
    @ApiOperation("根据商品id添加到购物车")
    public Result addShopping(@RequestBody ShoppingIdDTO shoppingIdDTO) {
        log.info("商品的id为：{}", shoppingIdDTO);
        shoppingService.addShopping(shoppingIdDTO.getId());
        return Result.success();
    }


    @PostMapping("/delete")
    @ApiOperation("批量删除购物车商品")
    public Result deleteShopping(@RequestBody List<Long> ids) {
        log.info("批量删除商品的id为：{}", ids);
        shoppingService.deleteShopping(ids);
        return Result.success();
    }

    @PostMapping("/amend")
    @ApiOperation("修改购物车商品数量")
    public Result amendShopping(@RequestBody ShoppingDhDTO shoppingDhDTO) {
        log.info("修改商品的信息为：{}", shoppingDhDTO);
        shoppingService.amendShopping(shoppingDhDTO);
        return Result.success();
    }


    @GetMapping("/queryAll")
    @ApiOperation("获取用户的全部购物车")    //TODO 数据库要改变 数据和分页不一致   只能偶数查 两偶一值  优化数据库
    public Result<PageResult> queryAllShopping(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        log.info("获取用户的全部购物车");
        //获取购物车分表里面的数据，商品的名字 描述 及其商品地址分表
        PageResult pageResult = shoppingService.queryAllShopping(page, pageSize);
        return Result.success(pageResult);
    }


}
